package com.yjhb.android.sbz.view;

import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.yjhb.android.sbz.entity.TaskStep;
import com.jojo.android.zxlib.tool.ZxTool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建者 zx
 * 创建时间 2020/11/30 9:08
 */
public class TaskStepView extends LinearLayout {
    private final String TAG = "TaskStepView";
    private onItemButtonClickListener onItemButtonClickListener;
    private List<TaskStep> steps;
    private Map<Integer, TaskStepItemView> stepViewMap;
    private boolean isCheck = false;

    public TaskStepView(Context context) {
        this(context, null);
    }

    public TaskStepView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TaskStepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
        Paint paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
    }

    public void refresh() {
        removeAllViews();
        if (steps != null) {
            stepViewMap = new HashMap<>();
            for (TaskStep step : steps) {
                final TaskStepItemView itemView = new TaskStepItemView(getContext(), step);
                itemView.setCheck(isCheck);
                stepViewMap.put(step.getId(), itemView);
                itemView.setOnButtonClickListener(new TaskStepItemView.onButtonClickListener() {
                    @Override
                    public void onButtonClick(int button) {
                        if (onItemButtonClickListener != null) {
                            onItemButtonClickListener.onItemButtonClick(itemView, button);
                        }
                    }
                });
                LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                params.bottomMargin = ZxTool.dip2px(35);
                addView(itemView, params);
            }
        }
        requestLayout();
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).setEnabled(enabled);
        }
    }

    public void setCheck(boolean check) {
        isCheck = check;
    }

    public void setSign(boolean b) {
        for (int i = 0; i < getChildCount(); i++) {
            TaskStepItemView taskStepItemView = (TaskStepItemView) getChildAt(i);
            taskStepItemView.setSign(b);
        }
    }

    public TaskStepItemView getItemView(Integer id) {
        if (id == null) return null;
        if (stepViewMap != null) {
            return stepViewMap.get(id);
        }
        return null;
    }

    public void setOnItemButtonClickListener(TaskStepView.onItemButtonClickListener onItemButtonClickListener) {
        this.onItemButtonClickListener = onItemButtonClickListener;
    }

    public interface onItemButtonClickListener {
        void onItemButtonClick(TaskStepItemView stepItemView, int button);
    }


    public void setTask(List<TaskStep> steps) {
        this.steps = steps;
    }
}
